<?php
  include('header.php');
  include('show_smilies.php');
  if ($bad_word_filter == 1) {
      include 'word_filter.php';
  }
  if(!is_numeric($_GET['u'])) {
      header('location: index.php');
      }
  if (!$_SESSION['signed_in'] and $guest_view_profile == 0) {
      header('location: signin.php');
  } else {
      $user_posts = mysql_real_escape_string($_GET['u']);
      if (mysql_real_escape_string($_GET['u']) == "") {
          echo "An error occured, please try again later.";
          die;
      }
      $hide_footer = -1;
      $counter = 0;
      $qqq = 1;
      $result = $db->query("SELECT * FROM " . $table_prefix . "users WHERE user_id =" . mysql_real_escape_string($_GET['u']) . "");
      if (!$result) {
          echo "An error occured";
          die;
      }
      $row = $db->fetch_array($result,'assoc');
      $user = $row['user_name'];
      $userid = $row['user_id'];
      
      
      $pageTitle = "$l_view " . $user . " $l_page_posts";
      $sort = "DESC";
      $bys = "post_date";
	  if (isset($_GET['sort']))
	  {
      if (mysql_real_escape_string($_GET['sort']) == "desc") {
          $sort = "DESC";
          $bys = "post_date";
      }
      if (mysql_real_escape_string($_GET['sort']) == "asc") {
          $sort = "ASC";
          $bys = "post_date";
      }
      if (mysql_real_escape_string($_GET['sort']) == "sub") {
          $sort = "ASC";
          $bys = "post_subject";
      }
	  }
?>
<!--<br />Sort by:
<select class="dropdown" name="sort" ONCHANGE="location = this.options[this.selectedIndex].value;">
<option value="view_new.php?sort=desc">-----</option>
<option value="view_new.php?sort=desc">Descending</option>
<option value="view_new.php?sort=asc" >Ascending</option>
<option value="view_new.php?sort=sub" >Post Subject</option>
</select><br />
-->
<?php
      echo "<b>$l_view_my</b><hr/>";
	  $check_approved = "AND post_approve = 0";
						  if ($_SESSION['user_level']>=1)
						  {$check_approved = "";}
      $result = $db->query("SELECT COUNT(*) FROM " . $table_prefix . "posts WHERE post_by = $user_posts $check_approved ORDER BY " . $bys . " " . $sort . "");
      if (!$result) {
          echo "An error occured, please try again later.";
          die;
      } else {
          $r = $db->fetch_row($result);
          $numrows = $r[0];
          $rowsperpage = $max_search_results;
          $totalpages = ceil($numrows / $rowsperpage);
          echo "<table><tr>";
          if (isset($_GET['page']) && is_numeric($_GET['page'])) {
              $currentpage = (int)$_GET['page'];
          } else {
              $currentpage = 1;
          }
          if ($currentpage > $totalpages) {
              $currentpage = $totalpages;
          }
          if ($currentpage < 1) {
              $currentpage = 1;
          }
          $offset = ($currentpage - 1) * $rowsperpage;
          $current_page = $page;
          if ($current_page == "") {
              $current_page = 1;
          }
          $range = $max_pagination_link;
          if ($range >= $totalpages) {
              $range = $totalpages;
          }
          if ($totalpages <= 0) {
              $totalpages = 1;
          }
          echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' '.$l_maches.']  &nbsp; &nbsp;';
          if ($currentpage > 1) {
              echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=1'><<</a> ";
              $prevpage = $currentpage - 1;
              echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&age=$prevpage'><</a> ";
          }
          for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
              if (($x > 0) && ($x <= $totalpages)) {
                  if ($x == $currentpage) {
                      echo " [<b>$x</b>] ";
                  } else {
                      echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$x'>$x</a> ";
                  }
              }
          }
          if ($currentpage != $totalpages) {
              $nextpage = $currentpage + 1;
              echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$nextpage'>></a> ";
              echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$totalpages'>>></a> ";
          }
          echo '</td></tr></table>';
         
          if ($page <= 0 or $page > $totalpages) {
              $page = 1;
          }
          
          $offset = $max_search_results * ($page - 1);
          
          $result2 = $db->query("SELECT * FROM " . $table_prefix . "posts WHERE post_by = $user_posts $check_approved ORDER BY " . $bys . " " . $sort . " LIMIT $offset, $max_search_results");
          if (!$result2) {
              echo "An error occured!";
              die;
          }
          $qqq += 1;
          //PREPARE BAD WORD FILTER BEFORE LOOP
          if ($bad_word_filter == 1) {
              $filter = $db->query("SELECT * FROM " . $table_prefix . "word_filter");
              $word_filter = mysql_fetch_assoc($filter);
          }
          while ($row2 = $db->fetch_array($result2,'assoc')) {
              $result3 = $db->query("SELECT topic_subject, topic_cat, topic_last_poster_id, reply FROM " . $table_prefix . "topics WHERE topic_id = " . $row2['post_topic'] . "");
              if (!$result3) {
                  echo "An error occured!";
                  die;
              }
              $row3 = $db->fetch_array($result3,'assoc');
              $last_poster = "<a href = 'user.php?u=" . $userid . "'>" . $user . "</a>";
              if ($userid == 1) {
                  $last_poster = "<i>" . $user . "</i>";
              }
              //To do: Fix this query bellow
              $result44 = mysql_query("SELECT cat_permission FROM " . $table_prefix . "categories WHERE cat_id = " . $row3['topic_cat'] . "");
              if ($result44) {
                  $row44 = $db->fetch_array($result44,'assoc');
              }
              
              if ($row44['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] >= 1) {
                  $hide_forum = 0;
              }
              if ($row44['cat_permission'] == 3 and $_SESSION['signed_in'] and $_SESSION['user_level'] < 1) {
                  $hide_forum = 1;
              }
              if ($row44['cat_permission'] == 3 and $_SESSION['signed_in'] == false) {
                  $hide_forum = 1;
              }
              if ($row44['cat_permission'] == 2 and $_SESSION['signed_in'] == false) {
                  $hide_forum = 1;
              }
              if ($row44['cat_permission'] == 2 and $_SESSION['signed_in']) {
                  $hide_forum = 0;
              }
              
              if ($row44['cat_permission'] <= 1) {
                  $hide_forum = 0;
              }  
              $cur_page = $row2['post_page'];

              //FILTER TECT
              $text = $row2['post_content'];
              if ($row2['post_smilies'] == 0) {
                  $text = smilies($text, 'img/smilies/');
              }
              //BAD WORD FILTER
              if ($bad_word_filter == 1) {
                  if ($word_filter['bad_words'] != "") {
                      $banned_word_list = $word_filter['bad_words'];
                      $text = wordfilter($banned_word_list, $text);
                  }
              }
              $text = str_replace('%69%66%72%61%6D%65', 'iframe', $text);
              $text = substr($text,0,400);
              if ($_SESSION['signed_in'] == false and $guest_show_links == 0) {
                  $ppage = mysql_real_escape_string($_GET['page']);
                  if ($ppage <= 0) {
                      $ppage = 1;
                  }
                  $text = preg_replace('#<a.*?>([^<]*)</a>#i', '<span style="color:red">' . $l_please_s . ' <a style="color:#CE0404;" href="signin.php?t=' . mysql_real_escape_string($_GET['t']) . '&page=' . $ppage . '">' . $l_login_s . '</a> ' . $l_or . ' <a style="color:#CE0404;" href="signup.php">' . $l_register_s . '</a> ' . $l_to_view . '</span>', $text);
              }
              
              if ($hide_forum != 1) {
                  echo "<table border='1'>
<tr>
<th> &nbsp;<a href='topic.php?t=" . $row2['post_topic'] . "&page=" . $cur_page . "#" . $row2['post_id'] . "'>" . $row3['topic_subject'] . "</a><b> </b> |  $l_posted_by  " . $last_poster . " $l_on " . date($date_format, strtotime($row2['post_date'])) . "
</th></tr> <tr><td class='postedText'><span>" . $text . "</span></tr></td><tr><td height='24' bgcolor='white'></tr></td><hr/>";
                  $counter += 1;
              }
          }
          // BOTTOM PAGINATION
          $range = $max_pagination_link;
          if ($numrows > 0) {
              if ($range >= $totalpages) {
                  $range = $totalpages;
              }
              echo '<td align="right" class="pagination"> <b>' . $l_page . ' ' . $current_page . ' ' . $l_page_of . ' ' . $totalpages . '</b> [' . $numrows . ' posts]  &nbsp; &nbsp;';
              if ($currentpage > 1) {
                  echo " <a title= '$l_first_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=1'><<</a> ";
                  $prevpage = $currentpage - 1;
                  echo " <a title='$l_prev_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$prevpage'><</a> ";
              }
              for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
                  if (($x > 0) && ($x <= $totalpages)) {
                      if ($x == $currentpage) {
                          echo " [<b>$x</b>] ";
                      } else {
                          echo " <a title='$l_goto_page " . $x . "' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$x'>$x</a> ";
                      }
                  }
              }
              if ($currentpage != $totalpages) {
                  $nextpage = $currentpage + 1;
                  echo " <a title='$l_next_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$nextpage'>></a> ";
                  echo " <a title='$l_goto_last_page' href='{$_SERVER['PHP_SELF']}?u=" . $user_posts . "&page=$totalpages'>>></a> ";
              }
              echo '</td></tr></table> ';
              // BOTTOM PAGINATION END
          }
          echo '</table><br />' . $l_search_found . ' ' . $numrows . ' ' . $l_smatches . '';
          $counter = 0;
      }
  }
  include 'footer.php';
  $pageContents = ob_get_contents();
  ob_end_clean();
  echo str_replace('<!--TITLE-->', $pageTitle, $pageContents);
?>